package com.coding.kan_yi_dao.service.impl;

import com.coding.kan_yi_dao.common.Result;
import com.coding.kan_yi_dao.entity.GoodsNew;
import com.coding.kan_yi_dao.entity.ShoppingCartItem;
import com.coding.kan_yi_dao.mapper.ShoppingCartItemMapper;
import com.coding.kan_yi_dao.service.IShoppingCartItemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 砍一刀，不买就砍你一刀
 * @since 2023-09-20
 */
@Service
public class ShoppingCartItemServiceImpl extends ServiceImpl<ShoppingCartItemMapper, ShoppingCartItem> implements IShoppingCartItemService {

   @Resource
   ShoppingCartItemMapper shoppingCartItemMapper;
    //全查询
    @Override
    public Result go_select(ShoppingCartItem shoppingCartItem) {
        Integer b = shoppingCartItem.getUserId();
        return Result.ok(shoppingCartItemMapper.go_selete_shopping(b));
    }

    //修改
    @Override
    public Result go_update(ShoppingCartItem shoppingCartItem) {
        return Result.ok(baseMapper.updateById(shoppingCartItem));
    }

    //删除
    @Override
    public Result go_delete(List<Integer> ids) {
        List<ShoppingCartItem> shoppingCartItems = new ArrayList<>();
        for (int i = 0; i < ids.size(); i++) {
            /*ShoppingCartItem shoppingCartItem = new ShoppingCartItem();
            shoppingCartItem.setGoodsId(ids.get(i));
            shoppingCartItem.setIsDeleted(0);
            shoppingCartItems.add(shoppingCartItem);*/

            Map<String,Object> map=new HashMap<>();
            map.put("goods_id",ids.get(i));
            baseMapper.deleteByMap(map);
        }
        /*System.out.println(goodsNewss);return Result.ok(this.update().setSql("goods_delete=0").in("goods_new_id",goodsNewss).update());*/
        return Result.ok("成功");
    }

    //添加
    @Override
    public Result go_insert(ShoppingCartItem shoppingCartItem) {
       List<ShoppingCartItem> a = query().eq("goods_id",shoppingCartItem.getGoodsId())
                .eq("user_id",shoppingCartItem.getUserId()).list();
        if (a.size()==0){
            return Result.ok(baseMapper.insert(shoppingCartItem));
        }
        shoppingCartItem.setGoodsCount(a.get(0).getGoodsCount()+shoppingCartItem.getGoodsCount());
        return Result.ok(baseMapper.insert(shoppingCartItem));
    }
}
